package org.example.service;

import com.fasterxml.jackson.annotation.JsonFormat;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.example.Blog.Blog;
import org.example.Blog.BlogDao;
import org.example.JDBC.Select;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@WebServlet("/blog_list")

public class BlogList extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        BlogDao blogDao=new BlogDao();
        ObjectMapper objectMapper=new ObjectMapper();
        resp.setContentType("application/json;charset=utf-8");
        String queryString=req.getParameter("blogId");
        if(queryString==null){
            List<Blog> list=blogDao.selectAll();
            System.out.println(objectMapper.writeValueAsString(list));
            resp.getWriter().write(objectMapper.writeValueAsString(list));
        }else{
            int blogId = Integer.parseInt(queryString);
            Select select=new BlogDao();
            Blog blog = select.selectById(blogId);
            System.out.println(blog);
            resp.getWriter().write(objectMapper.writeValueAsString(blog));
        }
    }
}
